package com.book.iphone;

import com.book.book.domain.BooklogDO;
import com.book.book.domain.QueueDO;
import com.book.book.domain.UsersDO;
import com.book.book.service.BooklogService;
import com.book.book.service.QueueService;
import com.book.book.service.UsersService;
import com.book.iphone.domain.DriftDo;
import com.book.iphone.domain.QueueListItem;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 作者：青松
 * 日期： 2018/1/30
 */
@RequestMapping("/iphone/driftDetial")
@Controller
//将要漂流的书
public class driftDetialController {
    @Autowired
    QueueService queueService;
    @Autowired
    UsersService usersService;
    @Autowired
    BooklogService booklogService;
    /**
     * @param userId   这本书此时在该用户手中
     * @param bookName 这本书的id
     *                 <p>
     *                 拿着这本书的书名和userid查到属于这本书的漂流队列，
     *                 通过用户id查到这本书流传到队列的位置，从而查到下一个用户的信息
     *                 如果该用户是最后一个，则漂流到这本书的主人的手中
     * @param model
     * @return
     */
    @RequestMapping(value = "/driftinfo")
    String getDriftInfo(String userId, String bookName,int id, Model model) {
        List<QueueDO> queueDOList = queueService.getQueueItem(userId, bookName);
        QueueDO queueDO = queueDOList.get(0);
        String json = queueDO.getQueue();
        Gson gson = new Gson();
        List<QueueListItem> items = gson.fromJson(json
                , new TypeToken<List<QueueListItem>>() {
                }.getType());
        QueueListItem item = new QueueListItem();
        if (items.size() == 1) {//将这本书的贡献者回显给用户，否则，将用户的下一站回显给用户
            String holderId = queueDO.getUserId();
            UsersDO holderUser = usersService.get(holderId);
            item.setUserId(holderId);
            item.setPhone(holderUser.getPhone());
            item.setNickName(holderUser.getNickName());
            item.setAddress(holderUser.getAddress());
        } else {
            item = items.get(1);
        }
        model.addAttribute("queueListItem", item);
        model.addAttribute("bookName",bookName);
        model.addAttribute("Id",id);
        model.addAttribute("queueId",queueDO.getId());
        return "book/phone/html/drift_detial";
    }

    @RequestMapping("/driftsubmit")
    String driftSubmit(QueueListItem item,String bookName
            ,int id,String text,String number,Integer queueId){

        BooklogDO booklogDO= booklogService.get(id);
        if (booklogDO!=null){
            booklogDO.setSentimentText(text);
            booklogDO.setNextId(item.getUserId());
            booklogDO.setStarTime(new Date());
            booklogDO.setWay(1);
            booklogDO.setTrackingNumber(number);
            booklogService.update(booklogDO);
            QueueDO queueDO=queueService.get(queueId);
            queueDO.setRedundance1("2");
            queueService.update(queueDO);
            //更新图书在漂流队列中的状态
        }
        return "redirect:/iphone/user";
    }
}
